package com.matrix.hellokafka.producerSendType;

import com.matrix.hellokafka.util.ConfigUtil;
import org.apache.kafka.clients.producer.Callback;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Objects;
import java.util.Properties;
import java.util.concurrent.Future;

public class KafkaFultureProduct {


    public static void main(String[] args) {



        Properties properties = ConfigUtil.getProducerProperties();

        KafkaProducer<String, String> producer = new KafkaProducer<String, String>(properties);

        try {
            ProducerRecord<String, String> record;
            for (int i = 0; i < 400; i++) {
                record = new ProducerRecord<>("my-topict", String.valueOf(i), "jyy==>"+i);
                Future<RecordMetadata> future = producer.send(record);
                System.out.println(i+"message is sent" );

                RecordMetadata recordMetadata = future.get();//阻塞在这里

                if(Objects.nonNull(recordMetadata)){
                    System.out.println(String.format("offset:%s,partition:%s,topic:%s",recordMetadata.offset(),recordMetadata.partition(),recordMetadata.topic() ));
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            producer.close();
        }
    }
}
